'use strict'

const Controller = require('egg').Controller
const svgCaptcha = require('svg-captcha')
class CaptchaController extends Controller {
  async codeforLogin() {
    const svgData = svgCaptcha.create()
    const insertData = await this.app.mysql.insert('code', {
      text: svgData.text,
    })
    this.ctx.body = { ...svgData, codeId: insertData.insertId } // 需要跟登陆接口配合
  }

  async login() {
    const { username, password, text, codeId } = this.ctx.request.body
    // 1. 先验证验证码对不对
    const code = await this.app.mysql.get('code', { codeId })
    if (text === code.text) {
      this.ctx.body = { code }
    } else {
      this.ctx.body = '验证码错误'
    }
  }
}

module.exports = CaptchaController
